import { ElMessageBox, ElMessage } from 'element-plus'
import { useUserStoreHook } from '@/store'
// 错误代码消息提示框
const tip = (message = 'Error') => {
  ElMessage.error({
    message,
    duration: 1000 * 2,
    type: 'error'
  })
}

// 错误代码捕获
export const errorHandler = (status: any) => {
  switch (status) {
    case 302:
      tip('Interface redirection')
      break
    case 400:
      tip('Request error')
      break
    case 401:
      tip('Not authorized. Please log in again')
      break
    case 403:
      tip('No operation permission')
      break
    case 404:
      tip('Request address error')
      break
    case 405:
      tip('Request method not allowed')
      break
    case 408:
      tip('Request timeout')
      break
    case 422:
      tip('An entity that cannot be processed')
      break
    case 500:
      tip('Server error')
      break
    case 501:
      tip('Service not implemented')
      break
    case 502:
      tip('Service restart')
      break
    case 503:
      tip('Service unavailable')
      break
    case 504:
      tip('The service is temporarily unavailable')
      break
    case 505:
      tip('HTTP versions are not supported')
      break
    default:
      tip('System error')
      break
  }
}

export const handleLogout = () => {
  ElMessageBox.confirm(
    'You have been logged out, you can cancel to stay on this page, or log in again',
    'Confirm logout',
    {
      confirmButtonText: 'Re-Login',
      cancelButtonText: 'Cancel',
      type: 'warning'
    }
  ).then(() => {
    // 在此处重新处理登出逻辑
    // store.dispatch('user/resetTokenAction').then(() => {
    //   location.reload()
    // })
    useUserStoreHook().logout()
    location.reload()
  })
}
